package com.mitei.fwk.ihm;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.mitei.fwk.fileTools.ExtensionFileFilter;
import com.mitei.fwk.messages.Messages;

public abstract class AbstractIHMFileChooser extends JPanel  implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GridBagLayout gridLayout = null;
	private GridBagConstraints constraints = null;
	private JTextField pathToFile = null;
	private JTextField pathToSave = null;
	private JButton selectImport = null;
	private JButton selectExport = null;
	private JButton save = null;
	private JLabel labelChooseOldFile = null;
	private JLabel labelChooseNewFile = null;
	private JFileChooser fileChooser = null;
	private File fileImport = null;
	private File fileExport = null;
	private ExtensionFileFilter filter = null;

	public AbstractIHMFileChooser() {
		this.pathToFile = new JTextField();
		this.pathToSave = new JTextField();
		this.fillPaths();
		this.selectImport = new JButton(Messages.ETC);
		this.selectExport = new JButton(Messages.ETC);
		this.save = new JButton(Messages.SAVE);
		this.labelChooseOldFile = new JLabel(Messages.FICHIER_A_MODIFIER_);
		this.labelChooseNewFile = new JLabel(Messages.SAUVEGARDER_SOUS_);
		
		this.selectImport.addActionListener(this);
		this.selectExport.addActionListener(this);
		
		this.gridLayout = new GridBagLayout();
		this.constraints = new GridBagConstraints();
		this.setLayout(this.gridLayout);
		
		addComposite(this.labelChooseOldFile, 0, 0, 1, 50);
		this.constraints.weightx = 7.0;
		addComposite(this.pathToFile, 0, 1, 1, 200);
		this.constraints.weightx = 1;
		addComposite(this.selectImport, 200, 1, 1, 10);
		addComposite(this.labelChooseNewFile, 0, 2, 1, 50);
		this.constraints.weightx = 7.0;
		addComposite(this.pathToSave, 0, 3, 1, 200);
		this.constraints.weightx = 1;
		addComposite(this.selectExport, 200, 3, 1, 10);
		
		Border etched = BorderFactory.createEtchedBorder();
		Border titled = BorderFactory.createTitledBorder(etched, Messages.CHOIX_DES_FICHIERS_);
		setBorder(titled);
	}
	
	private void addComposite(Component component, int x, int y, int height, int width) {
		this.constraints.gridx = x;
		this.constraints.gridy = y;
		this.constraints.gridheight = height;
		this.constraints.gridwidth = width;
		this.constraints.anchor = GridBagConstraints.WEST ;
		this.constraints.fill = GridBagConstraints.BOTH ;
		this.constraints.insets = new Insets(3, 3, 3, 3);
		add(component, this.constraints) ;	
	}
	
	private void fillPaths() {
		String name = System.getProperty ( "os.name" );
		if (name.startsWith("Windows")) {
			try {
				this.pathToFile.setText(System.getenv(Messages.HOMEDRIVE)
						+ "\\");
				this.pathToSave.setText(System.getenv(Messages.HOMEDRIVE)
						+ "\\");
			} catch (SecurityException e) {
				e.printStackTrace();
			}
		} else {
			this.pathToFile.setText("/");
			this.pathToSave.setText("/");
		}
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		
		if(event.getSource()== this.selectImport) {
			this.fileChooser = new JFileChooser();
			if(this.filter != null) {
				this.fileChooser.setFileFilter(this.filter);
			}
			int returnVal = fileChooser.showOpenDialog(this);
			 
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				this.fileImport = this.fileChooser.getSelectedFile();	
			}
			if(this.fileImport != null) {				
				this.pathToFile.setText(fileImport.getAbsolutePath());				
			}
		}
		
		if(event.getSource()== this.selectExport) {
			this.fileChooser = new JFileChooser();
			if(this.filter != null) {
				this.fileChooser.setFileFilter(this.filter);
			}
			int returnVal = fileChooser.showOpenDialog(this);
			 
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				this.fileExport = this.fileChooser.getSelectedFile();
			}
			
			if(this.fileExport != null) {
				this.pathToSave.setText(fileExport.getAbsolutePath());
			}
			
			boolean hasExtension = false;
			for(String s : this.filter.getExtensions()) {
				if(this.fileExport.getAbsolutePath().endsWith(s)) {
					hasExtension = true;
				}
			}
			if(!hasExtension) {
				String ext = this.pathToFile.getText().substring(
						this.pathToFile.getText().lastIndexOf("."), 
						this.pathToFile.getText().length());
				this.pathToSave.setText(this.pathToSave.getText() + ext);
			}
			
			
		}
		
		if(event.getSource()== this.getSave()) {
			this.saveAction();
		}
	}
	
	private void saveAction() {
		
	}

	public GridBagLayout getGridLayout() {
		return gridLayout;
	}

	public void setGridLayout(GridBagLayout gridLayout) {
		this.gridLayout = gridLayout;
	}

	public GridBagConstraints getConstraint() {
		return constraints;
	}

	public void setConstraint(GridBagConstraints constraint) {
		this.constraints = constraint;
	}

	public JTextField getPathToFile() {
		return pathToFile;
	}

	public void setPathToFile(JTextField pathToFile) {
		this.pathToFile = pathToFile;
	}

	public JTextField getPathToSave() {
		return pathToSave;
	}

	public void setPathToSave(JTextField pathToSave) {
		this.pathToSave = pathToSave;
	}

	public JButton getSelectImport() {
		return selectImport;
	}

	public void setSelectImport(JButton selectImport) {
		this.selectImport = selectImport;
	}

	public JButton getSelectExport() {
		return selectExport;
	}

	public void setSelectExport(JButton selectExport) {
		this.selectExport = selectExport;
	}

	public JButton getSave() {
		return save;
	}

	public void setSave(JButton save) {
		this.save = save;
	}

	public JLabel getLabelChooseOldFile() {
		return labelChooseOldFile;
	}

	public void setLabelChooseOldFile(JLabel labelChooseOldFile) {
		this.labelChooseOldFile = labelChooseOldFile;
	}

	public JLabel getLabelChooseNewFile() {
		return labelChooseNewFile;
	}

	public void setLabelChooseNewFile(JLabel labelChooseNewFile) {
		this.labelChooseNewFile = labelChooseNewFile;
	}

	public JFileChooser getFileChooser() {
		return fileChooser;
	}

	public void setFileChooser(JFileChooser fileChooser) {
		this.fileChooser = fileChooser;
	}

	public File getFile() {
		return fileImport;
	}

	public void setFile(File file) {
		this.fileImport = file;
	}

	public ExtensionFileFilter getFilter() {
		return filter;
	}

	public void setFilter(ExtensionFileFilter filter) {
		this.filter = filter;
	}

	public File getFileExport() {
		return fileExport;
	}

	public void setFileExport(File fileExport) {
		this.fileExport = fileExport;
	}
}
